/***
 * pjson Store
 * 扩展了：
 * datasetName：dataset名称,可以不填
 * baseHandler：基础查询Handler,可以通过 setBaseHandler()或者 getBaseHandler()来设置和获取
 * baseMethod：基础查询Handler中的方法,可以通过 setBaseMethod()或者 getBaseMethod()来设置和获取
 * baseConditions：基础查询条件。在每次发送请求时，都会带入的参数,
 * 可以在在store初始化时设置或者通过setBaseConditions()、addBaseCondition()、addBaseField()方法来设置
 * 
 * queryHandler：指定的查询Handler
 * queryMethod：指定的查询Handler中对应的方法名
 * queryConditions：动态查询条件,可以通过 ：addCondition(condition),addQueryField(fieldName,fieldValue)来添加
 * setQuery(handlerName,methodName,conditions)：设置查询对象,在store.load()方法前设置.
 * 其中：handlerName,methodName为对应的handler名称和方法名.conditions:为动态查询条件，不包括基础查询条件；
 */

Ext.define('Pearlsen.PJsonStore',  {
	extend: 'Ext.data.Store',
	alias: 'store.pJson',
	datasetName:"_queryDataSet",
	//基础查询对象
	baseHandler:null,
	baseMethod:null,
	baseConditions:[],
	//动态查询对象
	queryHandler:null,
	queryMethod:null,
	queryConditions:[],
	
	constructor: function(config) {
		config = config || {};
		this.setDatasetName(config.datasetName);
		this.setBaseQuery(config.handlerName, config.handlerMethod, config.baseConditions);
		this.setQuery(config.handlerName, config.handlerMethod, config.conditions);
		Ext.applyIf(config, {
			proxy:{
				type  : 'pajax',
				reader: {type: 'pjson'},
				writer: 'json',
				url:Pls.Webpath+'/query.asp',
				extraParams:this.getExtraParams()
			}
		});
		this.callParent([config]);
	},

	/***
	 * 获取查询参数对象
	 * @returns 
	 */
	getExtraParams:function(){
		var extraParams = {};
		extraParams.datasetName = this.datasetName;
		extraParams.handlerName= this.queryHandler;
		extraParams.handlerMethod = this.queryMethod?this.queryMethod:"handler";
		extraParams.conditions= this.getBaseConditions().concat(this.getQueryConditions());
		return extraParams;
	},
	/***
	 * 设置dataset名称
	 * @param datasetName
	 */
	setDatasetName:function(datasetName){
		if(Ext.isEmpty(datasetName)){
			this.datasetName="_queryDataSet";
		}else{
			this.datasetName = datasetName;
		}
	},
	setBaseQuery:function(baseHandler,baseMethod,baseConditions){
		this.setBaseHandler(baseHandler);
		this.setBaseMethod(baseMethod);
		this.setBaseConditions(baseConditions);
	},
	setBaseHandler:function(baseHandler){
		this.baseHandler = baseHandler;
	},
	setBaseMethod:function(baseMethod){
		this.baseMethod  = baseMethod;
	},
	/***
	 * 设置基础查询条件,
	 * @param conditions 可以为单个Condition()对象，或者Condition对象的数组
	 */
	setBaseConditions:function(conditions){
		this.getBaseConditions().splice(0, this.getBaseConditions().length);
		if(!conditions){
			conditions=[];
		}
		if(!Ext.isArray(conditions) ){
			this.baseConditions = [conditions];
		}else{
			this.baseConditions = conditions;
		}
	},
	/***
	 * 添加基础查询条件
	 * @param condition
	 */
	addBaseCondition:function(condition){
		var tempCondition = this.getBaseCondition(condition.fieldName);
		if(Ext.isEmpty(tempCondition)){
			this.getBaseConditions().push(condition);
		}else{
			tempCondition.value = condition.value;
		}
	},
	/***
	 * 添加基础查询条件
	 * @param fieldName
	 * @param value
	 */
	addBaseField:function(fieldName,value){
		var condition = new Condition(fieldName,value);
		this.addBaseCondition(condition);
	},
	getBaseHandler:function(){
		return this.baseHandler ;
	},
	getBaseMethod:function(){
		return this.baseMethod  ;
	},
	getBaseConditions:function(){
		return this.baseConditions ||[];
	},
	/***
	 * 设置查询条件
	 * @param handlerName
	 * @param methodName
	 * @param conditions
	 */
	setQuery:function(handlerName,methodName,conditions){
		this.setQueryHandler(handlerName);
		this.setQueryMethod(methodName);
		this.setQueryConditions(conditions);
	},
	/***
	 * 设置查询handler名
	 * @param queryHandler
	 */
	setQueryHandler:function(queryHandler){
		this.queryHandler = queryHandler;
	},
	/***
	 * 设置查询handler中的方法名
	 * @param queryMethod
	 */
	setQueryMethod:function(queryMethod){
		this.queryMethod = queryMethod;
	},
	setQueryConditions:function(conditions){
		this.clearCondition();
		if(!conditions){
			conditions=[];
		}
		if(!Ext.isArray(conditions) ){
			this.queryConditions = [conditions];
		}else{
			this.queryConditions = conditions;
		}
	},
	getQueryConditions:function(){
		return this.queryConditions || [];
	},
	
	/**
	 * 添加查询查询条件,如果字段名已经存在，那么覆盖旧的值
	 * @param fieldName
	 * @param value
	 */
	addQueryField:function(fieldName,value){
		var condition = new Condition(fieldName,value);
		this.addCondition(condition);
	},
	/***
	 * 添加查询条件，如果字段名已经存在，那么保留旧的值
	 * @param fieldName
	 * @param value
	 */
	addQueryFieldIf:function(fieldName,value){
		var condition = new Condition(fieldName,value);
		this.addConditionIf(condition);
	},
	/**
	 * 添加查询条件,如果字段名已经存在，那么覆盖旧的值
	 * @param condition
	 */
	addCondition:function(condition){
		var tempCondition = this.getCondition(condition.fieldName);
		if(Ext.isEmpty(tempCondition)){
			this.getQueryConditions().push(condition);
		}else{
			tempCondition.value = condition.value;
		}
	},
	/***
	 *  添加查询条件，如果字段名已经存在，那么保留旧的值
	 * @param condition
	 */
	addConditionIf:function(condition){
		var tempCondition = this.getCondition(condition.fieldName);
		if(Ext.isEmpty(tempCondition)){
			this.getQueryConditions().push(condition);
		}
	},	
	/**
	 * 获取查询条件对象List
	 * @returns {Array}
	 */
	getConditions:function(){
		return this.getQueryConditions();
	},
	/**
	 * 为了兼容之前的方法而保留方，设置查询条件List对象
	 * @param conditions
	 */
	setConditions:function(conditions){
		this.setQueryConditions(conditions);
	},
	/***
	 * 移除指定的查询字段
	 * @param fieldName
	 */
	removeCondition:function(fieldName){
		var queryConditions = this.getQueryConditions();
		var index = -1;
		for(var i=0;i<queryConditions.length;i++){
			var c = queryConditions[i];
			if( c.fieldName==fieldName ){
				index = i;
			}
		}
		if( index > -1 ){
			this.getQueryConditions().splice(index, 1);
		}
	},
	/***
	 * 清除查询条件
	 */
	clearCondition:function(){
		this.getQueryConditions().splice(0, this.getQueryConditions().length);
	},
	/***
	 * 获取指定名称的动态查询条件
	 * @param fieldName
	 * @returns
	 */
	getCondition:function(fieldName){
		var queryConditions = this.getQueryConditions();
		for(var i=0;i<queryConditions.length;i++){
			var c = queryConditions[i];
			if( c.fieldName==fieldName ){
				return c;
			}
		}
		return null;
	},
	/**
	 * 获取指定名称的基础查询条件
	 * @param fieldName
	 * @returns
	 */
	getBaseCondition:function(fieldName){
		var baseConditions = this.getBaseConditions();
		for(var i=0;i<baseConditions.length;i++){
			var c = baseConditions[i];
			if( c.fieldName==fieldName ){
				return c;
			}
		}
		return null;
	},
	/***
	 * 移除指定的基础查询字段
	 * @param fieldName
	 */
	removeBaseCondition:function(fieldName){
		var queryConditions = this.getBaseConditions();
		var index = -1;
		for(var i=0;i<queryConditions.length;i++){
			var c = queryConditions[i];
			if( c.fieldName==fieldName ){
				index = i;
			}
		}
		if( index > -1 ){
			this.getBaseConditions().splice(index, 1);
		}
	}
	
	
});